package zjq.day0109.lottery.dao.impl;

import zjq.day0109.lottery.dao.UserDao;
import zjq.day0109.lottery.entity.User;
import zjq.day0109.lottery.utils.BaseDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: maven-jdbc-01
 * @BelongsPackage: com.tmxk.lottery.dao.impl
 * @Author: zjq
 * @CreateTime: 2023-01-02  13:29
 * @Description: TODO
 * @Version: 1.0
 */
public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public List<User> queryAllUser(Map<String,Object> map) {
        List<User> users = new ArrayList<>();
        //2.创建dept对象，用来存储单条dept数据
        User user = null;
        //3.定义查询dept表所有数据的sql语句
        String sql = "select user_name,mark_no,status,phone,open_date from lottery";
        //4.调用公共的查询rs结果集方法
        super.rs = super.executeQuery(sql);
        //5.循环遍历rs结果集
        try {
            while (rs.next()) {
                //实例化dept对象
                user = new User();

                user.setUsername(rs.getString("user_name"));
                user.setMarkNo(rs.getString("mark_no"));
                user.setStatus(rs.getInt("status"));
                user.setPhone(rs.getString("phone"));
                user.setDate(rs.getString("open_date"));
                //将赋值后的单条对象数据存储到depts集合对象中
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAllConn(conn, pstmt, rs);
        }
        return users;
    }

    @Override
    public int saveUser(User user) {
        int addRes = 0;
        String sql = "insert into lottery(user_name,mark_no,status,phone,open_date) values(?,?,?,?,to_date(?,'yyyy-mm-dd'))";
        addRes = super.executeUpdate(sql, new Object[]{user.getUsername(), user.getMarkNo(), user.getStatus(), user.getPhone(), user.getDate()});
        return addRes;
    }

//    @Override
//    public List<User> queryAllUserByName(Map<String, Object> map) {
//        List<User> userList = new ArrayList();
//        //创建Schedule对象
//        User user = null;
//        //定义查询sql
//        StringBuffer sql = new StringBuffer();
//        sql.append("select mark_no,status,phone,open_date from lottery where user_name = ? ");
//        if (map.get("userName") != null) {
//            sql.append("AND USER_NAME = ? ");
//            rs = super.executeQuery(sql.toString(), new Object[]{map.get("userName")});
//        }else{
//        rs = super.executeQuery(sql.toString());
//    }
//        try{
//        while (rs.next()) {
//            //循环遍历rs结果集
//            user = new User();
//            user.setMarkNo(rs.getString("mark_no"));
//            user.setStatus(rs.getInt("status"));
//            user.setPhone(rs.getString("phone"));
//            user.setDate(rs.getString("open_date"));
//            user.setUsername(rs.getString("user_name"));
//            //将循环遍历的单个对象存储到集合对象中
//            userList.add(user);
//        }
//    } catch(
//    SQLException throwables){
//        throwables.printStackTrace();
//    } finally{
//        closeAllConn(conn, pstmt, rs);
//    }
//        return userList;
//    }
}



